package two_xia.myPainter;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StrokeChange extends JPanel {
    private DrawListener drawListener;
    private JLabel strokeLabel;

    public StrokeChange(DrawListener drawListener) {
        this.drawListener = drawListener;   // 从主窗口那里获取到监听器并且定义

        // 创建按钮
        JButton increaseButton = new JButton("变粗");
        JButton decreaseButton = new JButton("变细");
        JButton resetButton = new JButton("重置");

        // 创建文字展示区
        strokeLabel = new JLabel("当前粗细：" + drawListener.stroke);

        // 为按钮添加事件监听器
        increaseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                drawListener.stroke++;
                strokeLabel.setText("当前粗细：" + drawListener.stroke);
            }
        });

        decreaseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                drawListener.stroke = Math.max(1, drawListener.stroke - 1); // 不能小于1
                strokeLabel.setText("当前粗细：" + drawListener.stroke);
            }
        });

        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                drawListener.stroke = 1;
                strokeLabel.setText("当前粗细：" + drawListener.stroke);
            }
        });

        // 将按钮和文字展示区添加到面板
        add(increaseButton);
        add(decreaseButton);
        add(resetButton);
        add(strokeLabel);

        setVisible(true);
    }
}
